package com.adong.web;

import com.adong.pojo.User;
import com.adong.service.UserService;
import com.adong.service.impl.UserServiceImpl;
import com.adong.utils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

/**
 * 注册的servlet
 * 原来用来注册处理注册页面的验证
 * 现在和登录类都封装到userservlet中去了
 *
 * @author adong
 */
@Deprecated
public class RegistServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("username");
        String password = req.getParameter("password");
        String repwd = req.getParameter("repwd");
        String email = req.getParameter("email");
        String code = req.getParameter("code");


        //检查验证码是否正确

        if (code.equalsIgnoreCase("aa")) {

            //检查是否存在用户名
            if (userService.checkUserName(userName)) {

                //存在
                req.setAttribute("userName", userName);
                req.setAttribute("passWord", password);
                req.setAttribute("qpassWord", repwd);
                req.setAttribute("email", email);
                req.setAttribute("msg", "用户名已存在");
                //请求转发
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            } else {
                //不存在,注册成功
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
                userService.registerUser(new User(null,userName, password, email));
            }

        } else {
            req.setAttribute("userName", userName);
            req.setAttribute("passWord", password);
            req.setAttribute("qpassWord", repwd);
            req.setAttribute("email", email);
            if (code == null || code == "") {
                req.setAttribute("msg", "请输入验证码");
            } else {
                req.setAttribute("msg", "验证码错误");
            }
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
        }
        //关闭连接

    }
}
